Skip to content

feat: added admin get/delete/patch apis - #89

Merged
ayush00git merged 12 commits into
ayush00git:mainfrom
divyansh-v15-06:fix1
Jun 23, 2026
Merged

ayush00git merged 12 commits into
ayush00git:mainfrom
divyansh-v15-06:fix1

Conversation

@divyansh-v15-06

Copy link
Copy Markdown
Collaborator

Description
This PR resolves Issue #41 by implementing the missing API endpoints for editing, deleting, and fetching admin comments. As requested, these endpoints exclusively handle the comment logic and do not interact with or change the status of the posts.

Changes Included:
Added AdminEditComment Handler (PATCH):
Endpoint: PATCH /api/admin/comment/:type/:id/:comment_id
Allows admins to update the text content of their own comments.
Includes authorization checks to verify that the logged-in admin is the author of the comment (matched via email).
Added AdminDeleteComment Handler (DELETE):
Endpoint: DELETE /api/admin/comment/:type/:id/:comment_id
Allows admins to delete their own comments.
Includes the same authorization checks to prevent admins from deleting comments authored by others.
Added AdminGetComments Handler (GET):
Endpoint: GET /api/admin/comments
Fetches all comments authored by the currently logged-in admin across all posts. This resolves the AdminGetComments TODO left in the code.
Routing Updates: Successfully wired up the PATCH, DELETE, and GET endpoints in routes/admin.go and verified they are protected by the IsAuthenticated middleware.
Testing:
Passed the entire existing backend test suite (go test -v ./test/...).
Verified that comment ownership logic correctly restricts access to PATCH and DELETE actions.

@ayush00git

Copy link
Copy Markdown
Owner

@divyansh-v15-06 remove docs/ dir for now, api naming refactoring is planned so these docs would not make sense after that. And also update these apis in the frontend app/ dir.

@divyansh-v15-06

Copy link
Copy Markdown
Collaborator Author

done

@ayush00git ayush00git left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No API is binded to the app/ dir.

@ayush00git ayush00git changed the title Fix1 feat: added admin get/delete/patch apis Jun 22, 2026
@divyansh-v15-06

Copy link
Copy Markdown
Collaborator Author

@ayush00git check for the changes now

@ayush00git

Copy link
Copy Markdown
Owner

@divyansh-v15-06 wire the three new apis in the frontend app/ directory.

@ayush00git

Copy link
Copy Markdown
Owner
	e.GET("/api/admin/comments", middleware.IsAuthenticated(), h.AdminGetComments)
	e.PATCH("/api/admin/comment/:type/:id/:comment_id", middleware.IsAuthenticated(), h.AdminEditComment)
	e.DELETE("/api/admin/comment/:type/:id/:comment_id", middleware.IsAuthenticated(), h.AdminDeleteComment)

These APis need to be wired to the frontend

@divyansh-v15-06

Copy link
Copy Markdown
Collaborator Author

@ayush00git i have done the task in new commit check it

@ayush00git

Copy link
Copy Markdown
Owner

@ayush00git i have done the task in new commit check it

You've only defined the APIs there's no any wiring up of APIs in the frontend.

@divyansh-v15-06

Copy link
Copy Markdown
Collaborator Author

@ayush00git my bad never pushed the commit

@ayush00git

Copy link
Copy Markdown
Owner

@ayush00git my bad never pushed the commit

did you tested all three apis along with frontend ?

Comment thread handlers/admin_comment.go Outdated
postIDString := c.Param("id")
postID, err := strconv.ParseUint(postIDString, 10, 64)
if err != nil {
c.JSON(400, gin.H{"error": "invalid post id"})

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is supposed to be a error with status code 500.

	postID, err := strconv.ParseUint(postIDString, 10, 64)
	if err != nil {
		c.JSON(500, gin.H{"error": "failed parsing the post id"})
		return
	}

Comment thread handlers/admin_comment.go
commentID, err := strconv.ParseUint(commentIDString, 10, 64)
if err != nil {
c.JSON(400, gin.H{"error": "invalid comment id"})
return

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

500 here as well

Comment thread handlers/admin_comment.go

if comment.CommentableType != postType || comment.CommentableID != uint(postID) {
c.JSON(400, gin.H{"error": "comment does not belong to this post"})
return

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you explain me the significance of this check ? I think it is redundant.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you explain me the significance of this check ? I think it is redundant.

Since comments are modeled as a polymorphic association (CommentableType and CommentableID), comment IDs are unique within the database table but are detached from a strict hierarchy. The route structure matches RESTful sub-resources: /api/admin/comment/:type/:id/:comment_id.

Without verifying that comment.CommentableType == postType and comment.CommentableID == postID, a user could issue a request with a valid comment ID (comment_id) but supply parameters in the URL path (:type and :id) that point to a completely different post. This check ensures consistency between the requested URL route and the actual resource being updated/deleted in the database, preventing mismatched resource references.

Comment thread handlers/admin_comment.go
comment.UpdatedAt = time.Now()

result = h.DB.Save(&comment)
if result.Error != nil {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use h.DB.Model(&interface).Updates(comment) for PATCH type requests.

Comment thread handlers/admin_comment.go
c.JSON(400, gin.H{"error": "invalid post id"})
return
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto changes for the delete api.

… use Updates for PATCH, and keep delete API aligned
@divyansh-v15-06

Copy link
Copy Markdown
Collaborator Author

@ayush00git did the changes , checked the front end alignment with apis, its good to go

@ayush00git
ayush00git merged commit 0112fa1 into ayush00git:main Jun 23, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants